package com.example.androidlogin;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.palette.graphics.Palette;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private Toolbar mToolBar;
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private List<String> mTitles;
    private List<Fragment> mFragment;
    private List<Integer> list_iv;
    private List<String> list_tv1;
    private List<String> list_tv2;
    private List<String> list_tv3;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init();
        initViews();
    }

    private void initViews() {
        setSupportActionBar(mToolBar);

        mTitles.add("微信");
        mTitles.add("通讯录");
        mTitles.add("发现");
        mTitles.add("我");

        for (int i = 0; i < mTitles.size(); i++) {
            mTablayout.addTab(mTablayout.newTab().setText(mTitles.get(i)));
        }

        list_iv.add(R.drawable.one);
        list_iv.add(R.drawable.two);
        list_iv.add(R.drawable.three);
        list_iv.add(R.drawable.four);
        list_iv.add(R.drawable.five);
        list_iv.add(R.drawable.six);

        list_tv1.add("一世长安");
        list_tv1.add("一世长安");
        list_tv1.add("一世长安");
        list_tv1.add("一世长安");
        list_tv1.add("一世长安");
        list_tv1.add("一世长安");

        list_tv2.add("yscaysh");
        list_tv2.add("yscaysh");
        list_tv2.add("yscaysh");
        list_tv2.add("yscaysh");
        list_tv2.add("yscaysh");
        list_tv2.add("yscaysh");

        list_tv3.add("09:30");
        list_tv3.add("09:30");
        list_tv3.add("09:30");
        list_tv3.add("09:30");
        list_tv3.add("09:30");
        list_tv3.add("09:30");


        Fragment1 fragment1 = new Fragment1(list_iv, list_tv1, list_tv2, list_tv3);
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();

        mFragment.add(fragment1);
        mFragment.add(fragment2);
        mFragment.add(fragment3);
        mFragment.add(fragment4);

        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPageradapters(getSupportFragmentManager(), mFragment, mTitles);
        mTablayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(fragmentPagerAdapter);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Bitmap bitmap=null;
                if (position==0){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sun);
                }else if (position==1){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.tiankong);
                }else {
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.wake);
                }
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(@Nullable Palette palette) {
                        Palette.Swatch vibrant = palette.getVibrantSwatch();
                        if (vibrant == null) {
                            for (Palette.Swatch swatch : palette.getSwatches()) {
                                vibrant = swatch;
                                break;
                            }
                        }
                        int rgb = vibrant.getRgb();

                        mTablayout.setBackgroundColor(rgb);
                        mToolBar.setBackgroundColor(rgb);
                        if (Build.VERSION.SDK_INT > 21) {
                            Window window = getWindow();
                            window.setStatusBarColor(rgb);
                        }
                    }
                });
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void init() {
        mToolBar = findViewById(R.id.toolBar);
        mTablayout = findViewById(R.id.tabLayout);
        mViewPager = findViewById(R.id.viewPager);
        list_iv = new ArrayList<>();
        list_tv1 = new ArrayList<>();
        list_tv2 = new ArrayList<>();
        list_tv3 = new ArrayList<>();
        mTitles = new ArrayList<>();
        mFragment = new ArrayList<>();
    }
}
